package com.ayoka.mvp;

import android.text.TextUtils;

import com.ayoka.R;
import com.ayoka.base.BasePresenter;
import com.ayoka.base.IBaseView;
import com.ayoka.bean.BaseResponse;
import com.ayoka.bean.ResponseRefreshTokenBean;
import com.ayoka.bean.UserInfoBean;
import com.ayoka.bean.UserInformation;
import com.ayoka.http.API;
import com.ayoka.http.OkHttpListener;
import com.jcp.baselibrary.okhttp3.OkHttpUtil;
import com.jcp.baselibrary.utils.UIUtils;
import com.tendcloud.tenddata.TCAgent;
import com.tendcloud.tenddata.TDAccount;

/**
 * Created by gabriel on 2017/3/5.
 */

public interface LaunchMVP {

    interface LaunchModel {

    }

    interface ILaunchView extends IBaseView {

        void onLoginCallback(boolean isSuccess);

        void onRefreshTokenCallback(boolean isSuccess);
    }

    class LaunchPresenter extends BasePresenter<ILaunchView> {

        // 刷新Token
        public synchronized void refreshToken() {
            API.refreshToken(UserInformation.getInstance().user_info.ID, UserInformation.getInstance().refresh_token, new OkHttpListener<BaseResponse<ResponseRefreshTokenBean>>() {
                @Override
                public void onSuccess(BaseResponse<ResponseRefreshTokenBean> response) {
                    ResponseRefreshTokenBean responseRefreshTokenBean = response.data;
                    if (TextUtils.isEmpty(responseRefreshTokenBean.refresh_token) || TextUtils.isEmpty(responseRefreshTokenBean.session_token)) return;
                    UserInformation.getInstance().refresh_token = responseRefreshTokenBean.refresh_token;
                    UserInformation.getInstance().session_token = responseRefreshTokenBean.session_token;
                    UserInformation.save();
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().onRefreshTokenCallback(true);
                }

                @Override
                public void onFailure(int code, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().onRefreshTokenCallback(false);
                }
            });
        }

        public void loginByWechat(String openId) {
            API.doLoginByWX(null, openId, new OkHttpListener<BaseResponse<UserInformation>>() {
                @Override
                public void onSuccess(BaseResponse<UserInformation> response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    if (response.data == null){
                        mViewRef.get().onLoginCallback(false);
                    } else {
                        // 保存用户信息
                        UserInformation userInformation = response.data;
                        UserInformation.setUserInformation(userInformation);
                        /**
                         * TalkingData 监听微信登录
                         */
                        TCAgent.onLogin(userInformation.user_info.AppOpenID, TDAccount.AccountType.WEIXIN, userInformation.user_info.WeixinNickName);
                        mViewRef.get().onLoginCallback(true);
                    }
                }

                @Override
                public void onFailure(int code, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                    mViewRef.get().onLoginCallback(false);
                }
            });
        }


    }
}
